/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.api.importer.wizard;

import eu.easyedu.dictionary.api.importer.DictionaryImporter;
import eu.easyedu.dictionary.api.importer.impl.DictionaryImporterManager;
import java.io.File;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.openide.util.NbBundle;

public final class ImporterVisualPanel1 extends JPanel {

    /** Creates new form ImporterVisualPanel1 */
    public ImporterVisualPanel1() {
        initComponents();

    }

    @Override
    public String getName() {
        return NbBundle.getMessage(ImporterVisualPanel1.class, "ImporterVisualPanel1.name");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileFormatLabel = new javax.swing.JLabel();
        importerComboBox = new javax.swing.JComboBox();
        srcFileLabel = new javax.swing.JLabel();
        srcFileTextField = new javax.swing.JTextField();
        browseFileButton = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(fileFormatLabel, org.openide.util.NbBundle.getMessage(ImporterVisualPanel1.class, "ImporterVisualPanel1.fileFormatLabel.text")); // NOI18N

        importerComboBox.setModel(getImportersComboBoxModel());

        org.openide.awt.Mnemonics.setLocalizedText(srcFileLabel, org.openide.util.NbBundle.getMessage(ImporterVisualPanel1.class, "ImporterVisualPanel1.srcFileLabel.text")); // NOI18N

        srcFileTextField.setEditable(false);
        srcFileTextField.setText(org.openide.util.NbBundle.getMessage(ImporterVisualPanel1.class, "ImporterVisualPanel1.srcFileTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(browseFileButton, org.openide.util.NbBundle.getMessage(ImporterVisualPanel1.class, "ImporterVisualPanel1.browseFileButton.text")); // NOI18N
        browseFileButton.setPreferredSize(new java.awt.Dimension(20, 19));
        browseFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseFileButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(srcFileLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(fileFormatLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(srcFileTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browseFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(importerComboBox, 0, 250, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fileFormatLabel)
                    .addComponent(importerComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(srcFileLabel)
                    .addComponent(srcFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void browseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseFileButtonActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle(NbBundle.getMessage(ImporterVisualPanel1.class, "ImporterVisualPanel1.fileChooser.dialogTitle"));
        String ext = getImporter().getFormat();
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(ext + " Files", ext));
        int state = fileChooser.showOpenDialog(this);
        if (state == JFileChooser.APPROVE_OPTION) {
            srcFileTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_browseFileButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseFileButton;
    private javax.swing.JLabel fileFormatLabel;
    private javax.swing.JComboBox importerComboBox;
    private javax.swing.JLabel srcFileLabel;
    private javax.swing.JTextField srcFileTextField;
    // End of variables declaration//GEN-END:variables

    ComboBoxModel getImportersComboBoxModel() {
        DictionaryImporterManager manager = DictionaryImporterManager.getDefault();
        return new DefaultComboBoxModel(manager.getImporters().toArray());
    }

    public DictionaryImporter getImporter() {
        return (DictionaryImporter) importerComboBox.getSelectedItem();
    }

    public String getFile() {
        return srcFileTextField.getText();
    }
}

